<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Models\WxUniversity;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formPostTrait{
    public function index_post(Content $content)
    {
        return $content->title('笔记配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_post()->render()}</div>
HTML
            );
    }
    protected function form_post(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        global $__MINI_GLOBAL_TENANT_ID__;

        $form->tab('列表式', function (Form $form) {
            global $__MINI_GLOBAL_TENANT_ID__;
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">列表式布局生效</div>';
            }, '');
            $form->html(function () {
                return '<img style="width: 370px; height: auto;" src="https://img.mini.minisns.cn/images/biji/post/class.png" />';
            }, '');
            $form->radio('sticky_list_media_style', '媒体')->options([1 => '九宫格', 2 =>'满屏滑动'])->default(Settings::get('sticky_list_media_style', 2, true));
            $form->html(function () {
                return '<img style="width: 390px; height: auto;" src="https://img.mini.minisns.cn/images/biji/post/biji2.jpg" />';
            }, '');
            $form->text('sticky_list_foot_tool', '卡片底部')->default( Settings::get('sticky_list_foot_tool', '1|234', true) );
            if($__MINI_GLOBAL_TENANT_ID__ === 0) {
                $form->radio('post_list_footer_share_icon', '分享图标')->options(['e67e' => '<i class="mini-icon mini-fenxiang2"></i>', 'e60d' => '<i class="mini-icon mini-fenxiang5"></i>', 'e68d' => '<i class="mini-icon mini-fasong"></i>', 'e61e' => '<i class="mini-icon mini-fenxiang"></i>'])->help('每次更改需要重新下载编译前端')->default(trim(Settings::get('post_list_footer_share_icon', 'e67e'), '\\'));
            }
        });

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            $form->tab('充电', function (Form $form) {
                $form->radio('is_reward', '笔记是否开启充电')->options($this->options)->help('<a href="'.admin_url('settings/divide').'" target="_blank">平台分成比例设置</a>')->default(Settings::get('is_reward', 0));
            });
        }


        $form->tab('随机背景', function (Form $form) {

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips: 最少配置4个</div>';
            }, '');
            $form->image('sticky_random_bg_1', '随机背景1')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/4e44ed46b2d2747ee96b6c85991a61bd.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_1', '', true));
            $form->image('sticky_random_bg_2', '随机背景2')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/3111a1a825e8586330bc37f2bd35853d.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_2', '', true));
            $form->image('sticky_random_bg_3', '随机背景3')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/94dfb0867f2e7723a162dbcd17985fbc.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_3', '', true));
            $form->image('sticky_random_bg_4', '随机背景4')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_4', '', true));
            $form->image('sticky_random_bg_5', '随机背景5')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_5', '', true));
            $form->image('sticky_random_bg_6', '随机背景6')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_6', '', true));
            $form->image('sticky_random_bg_7', '随机背景7')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_7', '', true));
            $form->image('sticky_random_bg_8', '随机背景8')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_8', '', true));
            $form->image('sticky_random_bg_9', '随机背景9')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_9', '', true));
            $form->image('sticky_random_bg_10', '随机背景10')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_10', '', true));
            $form->image('sticky_random_bg_11', '随机背景11')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_11', '', true));
            $form->image('sticky_random_bg_12', '随机背景12')->help('建议图片 100*100 px。 <a href="https://img.mini.minisns.cn/2023/06/19/5682bebd708353e111b2a5f08f4fbe82.jpg" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('sticky_random_bg_12', '', true));

        });

        $form->tab('详情页', function (Form $form) {
            $form->textarea('sticky_content_statement', '内容声明')->help('在内容详情页笔记底部展示')->default(Settings::get('sticky_content_statement', '© 本文著作权归作者所有，未经许可，请勿转载', true));
            $form->number('sticky_reach_bottom_comment_page', '滑动加载评论页数')->help('若没有开启[相关推荐]，则配置为0即可，代表一直滑动加载，若开启了相关推荐，则推荐配置为1，代表滑动加载一次评论后，后面的评论手动点击加载')->default(Settings::get('sticky_reach_bottom_comment_page', 0, true));
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips: 开启相关推荐之后，在评论变成[点击加载]之后，才会开始加载相关推荐, 也就是说，滑动加载评论完的时候，才加载“相关推荐”。</div>';
            }, '');
            $form->radio('is_sticky_content_recommendation', '相关推荐')->options([0=>'关闭', 1=>'开启'])->help('暂时需要[付费1：搜索模块]后面更新后端时，再加普通的版本。')->default(Settings::get('is_sticky_content_recommendation', 0, true));

        });

        $form->tab('评论', function (Form $form) {
            $form->number('sticky_comments_child_num', '评论加载回复数量')->help('第一次加载时，加载的二级回复数量，超过这个数量才会出现 "展开更多回复"')->default(Settings::get('sticky_comments_child_num', 3, true));
            $form->radio('sticky_comment_force_bind_phone', '强制绑定手机号')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('sticky_comment_force_bind_phone', 0, true));
            $form->radio('sticky_comment_force_real_name', '强制实名')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('sticky_comment_force_real_name', 0, true));
            global $__MINI_GLOBAL_TENANT_ID__;
            $form->radio('sticky_comment_force_subscribe_mp', '强制关注公众号')->options([0=>'不强制', 1=>'强制'])->help($__MINI_GLOBAL_TENANT_ID__ > 0 ? '必须开启服务器地址' : '必须开启服务器地址，具体请去[全局配置-支付-微信-公众号]')->default(Settings::get('sticky_comment_force_subscribe_mp', 0, true));

            $form->multipleSelect('sticky_comment_force_university', '强制学生认证')->options(function ($sids) {
                if($sids){
                    $res = ['-1'=>'全域'];
                    foreach ($sids as $sid){
                        $university = WxUniversity::where('sid', $sid)->first();
                        if ($university) {
                            $res[$university->sid] = $university->name;
                        }
                    }
                    return $res;
                }else{
                    return ['-1'=>'全域'];
                }
            })->placeholder('请输入学校名字进行搜索')->ajax('select/universities')->default(Settings::get('sticky_comment_force_university', [], true));

        });

        $form->tab('视频', function (Form $form) {
            $form->html(function () {
                $res24 = __system__paycode__tip__(24);
                Tooltip::make('.mini_global_admin_open_not_support_tip')
                    ->bottom()
                    ->title($res24['tip']);
                return $res24['text'];
            }, '<b>一、仿抖音滑动视频开关</b>');
            $form->switch('sticky_video_slide_in_mp', '小程序')->help('完美')->default(Settings::get('sticky_video_slide_in_mp', 0, true));
            $form->switch('sticky_video_slide_in_h5', 'H5网页')->help('部分浏览器不能自动播放')->default(Settings::get('sticky_video_slide_in_h5', 0, true));
            $form->switch('sticky_video_slide_in_android_app', '安卓App')->help('暂不能评论和分享')->default(Settings::get('sticky_video_slide_in_android_app', 0, true));
            $form->switch('sticky_video_slide_in_ios_app', 'IOS App')->help('暂不能评论和分享')->default(Settings::get('sticky_video_slide_in_ios_app', 0, true));
        });

        $form->tab('分享至', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips: 有时候你在这里开启了，但前端没显示，不用怀疑别的，肯定是对应的appid啥的你没配置，QQ和微博的appid在生成前端的页面进行配置。</div>';
            }, '');
            $form->html(function () {
                return '';
            }, '<b>一、分享弹窗</b>');

            $form->checkbox('app_post_social_share_types', '社交分享')->options([
                'refer' => '转发',
                'wechat' => '微信',
                'timeline' => '朋友圈',
                'qq' => 'QQ',
                'qzone' => 'QQ空间',
                'weibo' => '微博',
            ])->default(Settings::get('app_post_social_share_types', ['refer', 'wechat', 'timeline', 'qq', 'qzone', 'weibo'], true));

            $form->checkbox('app_post_social_system_types', '系统功能')->options([
                'showCode' => '生成海报',
                'poster2' => '小海报',
                'edit' => '编辑',
                'copyUrl' => '复制链接',
                'feedback-notlike-1' => '不喜欢',
                'video-url' => '视频下载',
                'promotion' => '付费推广',
                'report' => '举报',
            ])->default(Settings::get('app_post_social_system_types', ['showCode', 'copyUrl', 'feedback-notlike-1', 'promotion', 'report'], true));

            $form->html(function () {
                return '';
            }, '<b>二、笔记海报</b>');
            $form->text('app_post_poster_bottom_tip', '海报底部提示')->help('<a href="https://img.mini.minisns.cn/images/publish/qrcode.png" target="_blank">示例效果</a>，默认值：❤ 扫一扫，找到你想要的生活')->default( Settings::get('app_post_poster_bottom_tip', '❤ 扫一扫，找到你想要的生活', true));
            $form->checkbox('app_post_poster_share_types', '海报分享媒介')->options([
                'wechat' => '微信',
                'timeline' => '朋友圈',
                'qq' => 'QQ',
                'qzone' => 'QQ空间',
                'weibo' => '微博',
                'save' => '保存图片',
            ])->default(Settings::get('app_post_poster_share_types', ['wechat', 'timeline', 'qq', 'qzone', 'weibo', 'save'], true));

        });

        $form->tab('笔记推广', function (Form $form) {
            global $__MINI_GLOBAL_TENANT_ID__;
            $form->decimal('app_promoted_post_refresh_cost', '刷新一次费用/一个时间点费用')->default(Settings::get('app_promoted_post_refresh_cost', ($__MINI_GLOBAL_TENANT_ID__ > 0 ? Settings::get('app_promoted_post_refresh_cost', 1.0) : 1.0), true));
            $form->divider();

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">解释：
<br/>1、推广的场景：<b>首页-推荐tab</b>，<b>圈子页-推荐tab</b>，<b>搜索页-热帖榜</b>
<br/>2、该模块依赖“计算热度”来实现，所以需要先解锁：<b>付费5 首页内容弱算法排序</b>
<br/>3、首页场景和圈子场景，还需要设置排序，<a href="https://img.mini.minisns.cn/hbx/paixu.jpg" target="_blank">点击查看</a></div>';
            }, '');

            $form->table('app_promoted_post_options','付费置顶', function (NestedForm $table) {
                $table->text('tip', '提示')->help('只是起到说明作用，比如：一天，一星期');
                $table->number('hour', '增加时间(小时)')->help('置顶的时长');
                $table->select('credit_type', '类型')->options([1=>'余额'])->default(1);
                $table->text('price', '价格');
            })->default(Settings::get('app_promoted_post_options', ($__MINI_GLOBAL_TENANT_ID__ > 0 ? Settings::get('app_promoted_post_options', '') : ''),true));

        });

        $form->tab('浏览量', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">我们有一个基础的浏览量，这个数据是真实的，我们可以选择根据其他表现数据，增加一些浏览量的展示， 这个虚拟的浏览量只给普通用户展示，超级管理员无效。</div>';
            }, '');
            $form->html(function () {
                return '';
            }, '<b>一、本身加成</b>');
            $form->decimal('sticky_views_conver_view_count', '+浏览数 / 每浏览人')->help('同一用户多次浏览算作一次')->default(Settings::get('sticky_views_conver_view_count', 0.0));
            $form->decimal('sticky_views_conver_img_count', '+浏览数 / 每图片')->default(Settings::get('sticky_views_conver_img_count', 0.0));
            $form->decimal('sticky_views_conver_video_count', '+浏览数 / 有视频')->default(Settings::get('sticky_views_conver_video_count', 0.0));
            $form->decimal('sticky_views_conver_vote_count', '+浏览数 / 有投票')->default(Settings::get('sticky_views_conver_vote_count', 0.0));
            $form->decimal('sticky_views_conver_time_minute_count', '+浏览数 / 每分钟')->help('会自动 * loge(n分钟) 的系数进行增长减缓')->default(Settings::get('sticky_views_conver_time_minute_count', 0.0));
            $form->divider();
            $form->html(function () {
                return '';
            }, '<b>二、笔记加成</b>');
            $form->decimal('sticky_views_conver_comment_count', '+浏览数 / 每评论')->default(Settings::get('sticky_views_conver_comment_count', 0.0));
            $form->decimal('sticky_views_conver_collect_count', '+浏览数 / 每收藏')->default(Settings::get('sticky_views_conver_collect_count', 0.0));
            $form->decimal('sticky_views_conver_liked_count', '+浏览数 / 每喜欢')->default(Settings::get('sticky_views_conver_liked_count', 0.0));
            $form->divider();
            $form->html(function () {
                return '';
            }, '<b>三、作者加成</b>');
            $form->decimal('sticky_views_conver_author_followed_count', '+浏览数 / 每粉丝')->default(Settings::get('sticky_views_conver_author_followed_count', 0.0));
            $form->decimal('sticky_views_conver_author_liked_count', '+浏览数 / 每被喜欢')->default(Settings::get('sticky_views_conver_author_liked_count', 0.0));
            $form->divider();
            $form->html(function () {
                return '';
            }, '<b>四、圈子加成</b>');
            $form->decimal('sticky_views_conver_circle_follow_count', '+浏览数 / 每粉丝')->default(Settings::get('sticky_views_conver_circle_follow_count', 0.0));
        });

        $form->action('settings/action/post');
        return $form;
    }

    public function post(Request $request){
        SiteUtils::forget_config_data();
        global $__MINI_GLOBAL_TENANT_ID__;
        $foot_tool = $request->input('sticky_list_foot_tool');
        $foot_tool_arr = explode('|', $foot_tool);
        $footer = [
            'left' => [],
            'right' => []
        ];
        if($foot_tool_arr && count($foot_tool_arr) == 2){
            Cache::forget(_multi_key('config:app:post:list:footer'));
            if($foot_tool_arr[0]){
                for ($i = 0; $i < strlen($foot_tool_arr[0]); $i++){
                    $footer['left'][] = (int)$foot_tool_arr[0][$i];
                }
            }
            if($foot_tool_arr[1]){
                for ($i = 0; $i < strlen($foot_tool_arr[1]); $i++){
                    $footer['right'][] = (int)$foot_tool_arr[1][$i];
                }
            }
            Cache::put(_multi_key('config:app:post:list:footer'), json_encode($footer));
        }

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            Settings::set('is_reward', $request->input('is_reward'), true);
        }


        Settings::set('sticky_list_media_style', $request->input('sticky_list_media_style'), true, true);
        Settings::set('sticky_list_foot_tool', $request->input('sticky_list_foot_tool'), true, true);

        Settings::set('sticky_random_bg_1', $request->input('sticky_random_bg_1'), true, true);
        Settings::set('sticky_random_bg_2', $request->input('sticky_random_bg_2'), true, true);
        Settings::set('sticky_random_bg_3', $request->input('sticky_random_bg_3'), true, true);
        Settings::set('sticky_random_bg_4', $request->input('sticky_random_bg_4'), true, true);
        Settings::set('sticky_random_bg_5', $request->input('sticky_random_bg_5'), true, true);
        Settings::set('sticky_random_bg_6', $request->input('sticky_random_bg_6'), true, true);
        Settings::set('sticky_random_bg_7', $request->input('sticky_random_bg_7'), true, true);
        Settings::set('sticky_random_bg_8', $request->input('sticky_random_bg_8'), true, true);
        Settings::set('sticky_random_bg_9', $request->input('sticky_random_bg_9'), true, true);
        Settings::set('sticky_random_bg_10', $request->input('sticky_random_bg_10'), true, true);
        Settings::set('sticky_random_bg_11', $request->input('sticky_random_bg_11'), true, true);
        Settings::set('sticky_random_bg_12', $request->input('sticky_random_bg_12'), true, true);

        Settings::set('sticky_content_statement', $request->input('sticky_content_statement'), true, true);
        Settings::set('sticky_reach_bottom_comment_page', _abs($request->input('sticky_reach_bottom_comment_page')), true, true);

        global $__MINI_GLOBAL_PAYCODE_1__;
        if($__MINI_GLOBAL_PAYCODE_1__){
            Settings::set('is_sticky_content_recommendation', $request->input('is_sticky_content_recommendation'), true, true);
        }

        Settings::set('sticky_comments_child_num', $request->input('sticky_comments_child_num'), true, true);
        Settings::set('sticky_comment_force_bind_phone', $request->input('sticky_comment_force_bind_phone'), true, true);
        Settings::set('sticky_comment_force_real_name', $request->input('sticky_comment_force_real_name'), true, true);
        Settings::set('sticky_comment_force_subscribe_mp', $request->input('sticky_comment_force_subscribe_mp'), true, true);

        Settings::set('sticky_comment_force_university', $request->input('sticky_comment_force_university'), true, true);

        $force_university = $request->input('sticky_comment_force_university');
        if(_empty_(end($force_university))){
            array_pop($force_university);
        }
        if(!_empty_($force_university)){
            $str = '';
            foreach ($force_university as $sid){
                if(!_empty_($str)){
                    $str .= '、';
                }
                $str .= WxUniversity::where('sid', $sid)->value('name');
            }
            Settings::set('sticky_comment_force_university_tip', $str, true, true);
        }

        Settings::set('app_post_poster_bottom_tip', $request->input('app_post_poster_bottom_tip'), true, true);
        Settings::set('app_post_social_share_types', $request->input('app_post_social_share_types'), true, true);
        Settings::set('app_post_social_system_types', $request->input('app_post_social_system_types'), true, true);
        Settings::set('app_post_poster_share_types', $request->input('app_post_poster_share_types'), true, true);
        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            Settings::set('post_list_footer_share_icon', $request->input('post_list_footer_share_icon'), true);
        }

        Settings::set('sticky_video_slide_in_mp', $request->input('sticky_video_slide_in_mp') == 1 ? 1 : 0, true, true);
        Settings::set('sticky_video_slide_in_h5', $request->input('sticky_video_slide_in_h5') == 1 ? 1 : 0, true, true);
        Settings::set('sticky_video_slide_in_android_app', $request->input('sticky_video_slide_in_android_app') == 1 ? 1 : 0, true, true);
        Settings::set('sticky_video_slide_in_ios_app', $request->input('sticky_video_slide_in_ios_app') == 1 ? 1 : 0, true, true);

        Settings::set('app_promoted_post_refresh_cost', _between_($request->input('app_promoted_post_refresh_cost'),0, 100000), true, true);
        Settings::set('app_promoted_post_options', AdminUtils::_table_data($request->input('app_promoted_post_options')), true, true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
